package org.example.ThreadDemo;

/**
 * @Author : zhangbo
 * @data : 2024/7/2
 * @description :
 */
public class MyThread2 extends Thread{
    volatile boolean flag = false ;     // 线程执行的退出标记
    @Override
    public void run() {
        while(!flag) {
            System.out.println("MyThread...run...");
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    public static void main(String[] args) throws InterruptedException {
        // 创建MyThread对象
        MyThread t1 = new MyThread() ;
        t1.start();
        // 主线程休眠2秒
        Thread.sleep(6000);
        // 调用interrupt方法
        t1.interrupt();
    }
}
